def f (x):
    for i in range(2,x):
        if x % i == 0:
            return (0)
    return(x) 
print ("Введите четерзначное число")
z = int(input())
if z < 1000 or z > 9999:
    print('0')
else:            
    u = z % 10
    d = (z // 10) % 10
    v = (z // 100) % 10
    h = z // 1000
    hat = [0] * 10
    hat[0] = u
    hat[1] = v
    hat[2] = h
    hat[3] = d
    hat[4] = h * 10 + v
    hat[5] = v * 10 + d
    hat[6] = d * 10 + u
    hat[7] = h * 100 + v * 10 + d
    hat[8] = v * 100 + d * 10 + u
    hat[9] = z
    maxi = 0
    for i in hat:
        n = f(i)
        if n != 0 and n > maxi:
            maxi = n
    print(maxi)